package com.yc.blog.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.yc.blog.service.HandleServiceAbstract;

@Component
public class HandleMapFactory implements ApplicationContextAware {
	
	private static Map<String, HandleServiceAbstract> handleMap;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, HandleServiceAbstract> clazs = applicationContext.getBeansOfType(HandleServiceAbstract.class);//获得所有HandleServiceAbstract的子类
		for (Entry<String, HandleServiceAbstract> entry : clazs.entrySet()) {
			handleMap.put(entry.getKey(), entry.getValue());
		}
	}
	
	/**
	 * 通过类型找到HandleService实现类
	 * @param key
	 * @return
	 */
	public static HandleServiceAbstract getHandleUtil(String key) {
		return handleMap.get(key);
	}

}
